/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.controller;

import java.io.File;
import java.io.FilenameFilter;
import java.util.List;

import uk.org.aspellclark.agenedb.gedcom.Gedcom;
import uk.org.aspellclark.agenedb.gedcom.model.Multimedia;
import android.net.Uri;

public class MultimediaController extends AbstractDB4OController {

	private String gedomPathDir = ".";

	//	private Bitmap getImageForFile(String filename) {
	//		Uri[] imgUris = this.getImagesForGedcom();
	//		Bitmap bmp = Bitmap.();
	//
	//		for (int x = 0; x < imgUris.length; x++) {
	//			if (imgUris[x].toString().contains(filename)) {
	//				bmp.setImageURI(imgUris[x]);
	//				break;
	//			}
	//		}
	//		return bmp;
	//	}

	/**
	 * @param dbCntrlr
	 */
	public MultimediaController(Db4oController dbCntrlr) {
		super(dbCntrlr);
	}

	public void addMultimedia(Multimedia obj) {
		this.dbCntrlr.db.store(obj);
	}

	public String getGedomPathDir() {
		return this.gedomPathDir;
	}

	private Uri[] getImagesForGedcom() {

		File images = new File(this.gedomPathDir);
		File[] imagelist = images.listFiles(new FilenameFilter() {

			public boolean accept(File dir, String name) {
				return ((name.endsWith(".jpg")) || (name.endsWith(".png")));
			}
		});
		String[] mFiles = new String[imagelist.length];

		for (int i = 0; i < imagelist.length; i++) {
			mFiles[i] = imagelist[i].getAbsolutePath();
		}
		Uri[] mUrls = new Uri[mFiles.length];

		for (int i = 0; i < mFiles.length; i++) {
			mUrls[i] = Uri.parse(mFiles[i]);
		}
		return mUrls;
	}

	public Multimedia getMultimediaRecord(String xref) {
		return Gedcom.getInstance().getMultimedia(xref);
	}

	/**
	 * @return
	 */
	public List<Multimedia> getMultimediaRecords() {
		List<Multimedia> objList = this.dbCntrlr.db.query(Multimedia.class);

		return objList;
	}

	public void setGedomPathDir(String gedomPathDir) {
		this.gedomPathDir = gedomPathDir;
	}
}
